﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Parsers.SpeechSynthesizer;
using SpeechSynthesizer.Parsers;

namespace SpeechSynthesizer
{
    public partial class Form1 : Form
    {
        //private string ResultFile = @"d:\Projects\C#\speech-synthesizer\ speech-synthesizer\SpeechSynthesizer\Result\out.wav";
        private string ResultFile = Directory.GetCurrentDirectory() + "\\Result\\out.wav";
        //private string TextRepository = @"d:\Projects\C#\speech-synthesizer\ speech-synthesizer\SpeechSynthesizer\text\";
        private string TextRepository = Directory.GetCurrentDirectory() + "'\text";

        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog.FileName = inputText.Text;
            if(saveFileDialog.ShowDialog()==DialogResult.OK)
            {
                if (!saveFileDialog.FileName.Contains(".wav"))
                    saveFileDialog.FileName += ".wav";
                File.Copy(ResultFile, saveFileDialog.FileName);
            }
        }

        private void inputText_TextChanged(object sender, EventArgs e)
        {
            if (!inputText.Text.Equals(""))
                playBtn.Enabled = true;
            else
            {
                if (playBtn.Enabled)
                    playBtn.Enabled = false;
            }
        }

        private void playBtn_Click(object sender, EventArgs e)
        {
            string inputTextStr = inputText.Text;

            InputTextParser inputParser = new InputTextParser(inputTextStr);
            inputParser.Parse();

            SyllablesParser syllablesParser = new SyllablesParser(inputParser.Letters);
            syllablesParser.Parse();

            //out text in dialog
            /*
            inputText.Clear();
            foreach (string syllable in syllablesParser.Syllables)
            {
                inputText.AppendText(syllable+"+");
            }
             */

            FilesParser filesParser = new FilesParser(syllablesParser.Syllables);
            filesParser.Parse();

            YPlayer.Play(filesParser.Files);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = "";
            openFileDialog.InitialDirectory = TextRepository;
            DialogResult result = openFileDialog.ShowDialog();
            if(result==DialogResult.OK)
            {
                string file = openFileDialog.FileName;
                try
                {
                    string text = File.ReadAllText(file);
                    inputText.Clear();
                    inputText.Text = text;
                }
                catch(IOException)
                {
                }
            }
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Упішыце або адкрыйце гатовы тэкст і націсніце 'Прайграць'."+
                "\nПраслухайце атрыманы тэкст.\nРаспрацоўшчык: Якімчык Сяргей", "Дапамога", MessageBoxButtons.OK);
        }
    }
}
